/**
 * @param {int} order_type 类型
 * @param {int} card_id  套餐的id
 * @param {String} sku_name 商品名称
 * @param {decimal} real_amount 实际价格
 * @param {decimal} amount 划线价格
 * @param {int} mobile 手机号
 * @param {String} creater 创建者名字
 * @param {int} user_id 用户id
 * @param {datetime} end_time 结束时间
 * @param {String} remark 备注
 * @param {String} openid openid
 * @param {String} payWay alipay 或者 wxpay
 * 
 * @param {Object} fn
 */ 
import NetTool from './axion.js'
import mNetTool from "../../tools/NetTool.js"
const appInfo = require ('../../appInfo')
var apis = require('../../tools/apis.js');
var _mNetTool = new NetTool
let providerList = []
let payWayIndex = 0
export const payfor = (data,payWay,fn)=>{
	uni.showLoading({
		title:'请稍等',
		mask:true
	})
	if(payWay == 'alipay'){
		payWayIndex = 0
	}else if(payWay == 'wxpay'){
		payWayIndex = 1
	}else{
		uni.$u.toast('支付渠道错误！')
		return
	}
	// 初始化参数
	// #ifdef APP-PLUS
	uni.showLoading({
		title:'请稍等...',
		mask:true
	})
	uni.getProvider({
	    service: "payment",
	    success: (e) => { 
	         providerList = [];
	        e.provider.map((value) => {
	            switch (value) {
	                case 'alipay':
	                    providerList.push({
	                        name: '支付宝',
	                        id: value,
	                        loading: false
	                    });
	                    break;
	                case 'wxpay':
	                    providerList.push({
	                        name: '微信',
	                        id: value,
	                        loading: false
	                    });
	                    break;
	                default:
	                    break;
	            }
	        })
	       
	    },
	    fail: (e) => {
			uni.hideLoading()
			uni.$u.toast("获取支付通道失败：" + e)
	        console.log("获取支付通道失败：", e);
	    }
	});
	// #endif
	 // 先存一个订单
	 uni.hideLoading()
	 uni.showLoading({
	 	title:'创建订单中...',
		mask:true
	 })
	_mNetTool.save('capi/V2/vip_order/orderMain/',{
		...data
	},res=>{
		 
		if(res.success){
			console.log('创建订单信息成功',res)
			uni.hideLoading()
			uni.showLoading({
				title:'请稍等...',
					mask:true
			})
			buyOnline(res.id,data.amount,data.openid,data.sku_name,success=>{
				
			},err=>{
				console.log('err',res)
			})
		}else{
			console.log("创建订单信息失败", orderInfo);
			uni.hideLoading()
			uni.showModal({
			    content: "创建订单信息失败",
			    showCancel: false
			})
			return;
		}

	})
	/**
	 * 
	 * @param {订单id} id 
	 * @param {订单金额} amount 
	 * @param {用户的openid} open_id 
	 * @param {支付成功的回调} fn 
	 * @param {支付失败回调} fn2
	 */ 
	let buyOnline = (id, amount, open_id, sku_name, fn, fn2)=>{
		console.log('buyOnline',{
			 open_id: open_id,
			 server_ip: "118.26.175.56",
			 amount: amount,
			 wxNotifyUrl: appInfo.wxNotifyUrl,
			 update_api: 1,
			 order_id: id, 
			 sku_name: sku_name, 
		}) 
		_mNetTool.get('capi/wcoa/order.do',{
			 open_id: open_id,
			 server_ip: "118.26.175.56",
			 amount: amount,
			 wxNotifyUrl: appInfo.wxNotifyUrl,
			 update_api: 1,
			 order_id: id,
			 sku_name: sku_name,
			config_business_id : '1028_2',
			payType:"app"
		},res=>{
			console.log('=================order',res)
			toPayment(res.rows, res => {
				console.log('toPayment',res)
		      fn(res)
		    }, fn2)
		  },r=>{
			  console.log('r?',r)
		  },rr=>{
			  console.log('rr?',rr)
		  }) 
	}
	let toPayment = (obj, fn, fn2)=>{ 
		console.log(obj)
		// let data = { 
		// 	"appid":obj.appId,
		// 	"timestamp": obj.timeStamp,
		// 	"noncestr": obj.nonceStr,
		// 	"package": "Sign=WXPay",
		// 	'partnerid': "1622689770", 
		// 	'prepayid': obj.prepay_id, 
		// 	"sign":obj.paySign
		// }
		let data = obj
		console.log('datadatadata',data) 
		uni.requestPayment({
		    provider: providerList[0].id,
		    orderInfo: data,
		    success: (e) => {
		        console.log("success", e);
				uni.hideLoading()
		        uni.showToast({
		            title: "感谢您的购买!"
		        })
				 typeof fn == "function" && fn(e)
		    },
		    fail: (e) => {
				uni.hideLoading()
		        console.log("fail", e);
		        uni.showModal({
		            content: "支付失败" ,
		            showCancel: false
		        })
				 typeof fn2 == "function" && fn(e)
		    },
		    complete: () => {
				
		    }
		})
	}
}
